#include "PreCompile.hpp"
#include "TestProjectLoading.hpp"

#include <direct.h>

namespace BuildNG
{
  void TestProjectLoading::simple_loading_test()
  {
    Glib::RefPtr<Project> project = Project::load_from_file("TestFiles/SimpleTest.buildng");
    assert(project->get_name() == "SimpleTest");

    assert(project->get_num_modules() == 1);
    Glib::RefPtr<Module> module = project->get_modules_list()[0];
    assert(module);
    assert(module->get_name() == "ContentModule");
    assert(project->get_module("ContentModule") == module);

    Glib::RefPtr<ContentModule> content_module = 
      Glib::RefPtr<ContentModule>::cast_dynamic(module);
    assert(content_module);
  }

  void TestProjectLoading::test_project_root_dir()
  {
    Glib::ustring expected_root_dir = Glib::build_filename(
      Glib::get_current_dir(),"TestFiles");

    {
      Glib::RefPtr<Project> project = Project::load_from_file("TestFiles/SimpleTest.buildng");
      assert(project->get_root_dir() == expected_root_dir);
    }

    _chdir("TestFiles");
    {
      Glib::RefPtr<Project> project = Project::load_from_file("SimpleTest.buildng");
      assert(project->get_root_dir() == expected_root_dir);
    }

    _chdir("Dum");
    {
      Glib::RefPtr<Project> project = Project::load_from_file("../SimpleTest.buildng");
      assert(project->get_root_dir() == expected_root_dir);
    }

    _chdir("..");
    _chdir("..");
  }

  void TestProjectLoading::test_name_collision()
  {
    try
    {
      Glib::RefPtr<Project> project = Project::load_from_file("TestFiles/NameCollisionTest.buildng");
      assert(!"An exception should have been thrown.");
    }
    catch(const NameCollisionException&)
    {
      // the expected behaviour
    }
  }
}
